/**
 @param arr 默认传参格式为[a,b,c]表示默认配置参数为''。[a,[b,[]],c]表示b指定参数类型为数组。如需验证 [[a,y]] 或者 [[a,{},y]表示a指定为对象并且进行验证
**/
function config(...arr) {
	let obj = {}
	arr.forEach(val => {
		let temp = {
			name: '', //存储对应字段的称号以便验证提示
			value: '',
			check: false
		}
		if (Object.prototype.toString.apply(val) == '[object Array]') {
			if (val.length < 5) {
				if (val.length == 2) {
					if (Object.prototype.toString.apply(val[1]) === '[object Boolean]') {
						temp.check = val[1]
					} else {
						temp.value = val[1]
					}
				} else {
					if (Object.prototype.toString.apply(val[1]) === '[object Boolean]') {
						temp.value = val[2]
						temp.check = val[1]
					} else {
						temp.value = val[1]
						temp.check = val[2]
					}
					if (val.length == 4) { //设置字段对应称号
						temp.name = val[3]
					}
				}
				obj[val[0]] = temp
			}
		} else {
			obj[val] = temp
		}
	})
	return obj
}

function check(obj, boolean) {
	let result = {}
	for (let key in obj) {
		if (obj[key].check) {
			if ((Object.prototype.toString.apply(obj[key].value) === '[object String]') && obj[key].value === '') {
				result = {
					check: false,
					name: obj[key].name || '',
					key,
					value: obj[key].value,
					type: 'String'
				}
				return result
			} else if ((Object.prototype.toString.apply(obj[key].value) === '[object Array]') && obj[key].value
				.length === 0) {
				result = {
					check: false,
					name: obj[key].name || '',
					key,
					value: obj[key].value,
					type: 'Array'
				}
				return result
			} else if ((Object.prototype.toString.apply(obj[key].value) === '[object Object]') && Object.keys(obj[key]
					.value).length === 0) {
				result = {
					check: false,
					name: obj[key].name || '',
					key,
					value: obj[key].value,
					type: 'Object'
				}
				return result
			}
		}
	}
	let tempObj = {}
	if (boolean) { //如果选择格式化则进行处理，否则默认返回
		for (let key in obj) {
			tempObj[key] = obj[key].value
		}
	}
	result = {
		check: true,
		form: boolean ? tempObj : obj,
		type: Object.prototype.toString.apply(obj)
	}
	return result
}
module.exports = {
	config,
	check
}
